package com.dries.portal.admin.bo;

import com.dries.portal.admin.entity.UmsAdmin;
import com.dries.portal.admin.entity.UmsResource;
import com.dries.portal.admin.enums.AdminStatusEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.social.security.SocialUserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 用户详情
 * @Author ZSY
 * @createTime 2020/9/23 16:52 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AdminUserDetails implements UserDetails, SocialUserDetails {
    private static final long serialVersionUID = 7116398307518304355L;

    private UmsAdmin admin;
    private List<UmsResource> resourceList;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 返回当前用户的角色信息
        return resourceList.stream()
                .map(role ->
                        new SimpleGrantedAuthority(role.getId() + ":" + role.getName()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return admin.getPassword();
    }

    @Override
    public String getUsername() {
        return admin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return admin.getStatus().equals(AdminStatusEnum.ACCOUNT_ENABLE.getCode());
    }

    @Override
    public String getUserId() {
        return admin.getUsername();
    }
}
